package homework;


import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import java.io.File;
import java.io.FileFilter;

/**
 * 视频时间计算
 * 以前运行是好的 现在再次运行报：Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
 * 	at ws.schild.jave.MultimediaObject.<clinit>(MultimediaObject.java:14)
 * 	at homework.GetAvi.extracted(GetAvi.java:51)
 * 	at homework.GetAvi.traverseFolder2(GetAvi.java:42)
 * 	at homework.GetAvi.main(GetAvi.java:17)
 * Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
 */
public class GetAvi implements FileFilter {
    public static void main(String[] args) throws Exception {
        for (int i = 1; i <= 11; i++) {
            String name = "D:/P2P_ziliao/day" + i;
            //getAllFilePath(name);
            
         System.err.println(name+":  "+traverseFolder2(name)); 
           
        }

        

    }
    public static long traverseFolder2(String path) throws Exception {
        

        File file = new File(path);
        long s=0;
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                //System.out.println("文件夹是空的!");
                return 0;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                       // System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath());
                        s+=extracted(file2.getAbsolutePath());
                    } else {
                        //System.out.println("文件:" + file2.getAbsolutePath());
                        s+=extracted(file2.getAbsolutePath());
                    }
                }
            }
        } 
        return s;
        
    }
    private static long extracted(String name) throws EncoderException {
        MultimediaObject multimediaObject = new MultimediaObject(new File(name));
        MultimediaInfo info = multimediaObject.getInfo();
        long playTime = info.getDuration();
        return playTime = playTime / 1000 / 60;
    }

    @Override
    public boolean accept(File file) {
        if(file.isDirectory()) {
            return true;  }
        else  
        {  
            String name = file.getName();  
            if(name.endsWith(".avi") )  {
                return true; }
            else  {
                return false;  }
        }  
    }
    
    /** 
     * get all the music file in the rootpath. 
     * @param rootPath 
     */  
    public static void getAllFilePath(String rootPath)  
    {  
          
        File file = new File(rootPath);  
        File[] files = file.listFiles(new GetAvi());  
        for(int i=0;i<files.length;i++)  
        {  
            if(files[i].isDirectory())  
            {  
                getAllFilePath(files[i].getPath());  
                //System.err.println("day"+i+":  "+extracted(name)); 
            } else {
               // System.err.println(files[i].getPath());
            }
            
        }  
          
              
    }  

}
